package com.twu.biblioteca;

import java.util.Scanner;

/**
 * Created by thais on 1/17/14.
 */
public class Menu {

    private Manager manager;

    public Menu(Manager manager) {
        this.manager = manager;
    }

    public void sayHello() {

        System.out.println(manager.sayHello());
    }

    public void listAvaiableMovies() {
        for(Movie m: manager.getAllAvailableMovies()){
            System.out.println(m.getName());
        }
    }

    public void listAvaiableBooks() {
        for(Book b: manager.getAllAvailableBooks()){
            System.out.println(b.getName());
        }

    }

    public void createABibliotecaManager(){
        manager.add(new Book("The Hobbit", true));
        manager.add(new Book("Head First Java", true));
        manager.add(new Book("Oxford Dictionary", true));
        manager.add(new Book("Game of Thrones", false));
        manager.add(new Book("Garfield", false));
        manager.add(new Book("Pokemon Guide", false));
    }

    public void listAllInitialOptions() {
        MenuOptions menuOptions = new MenuOptions();
        menuOptions.getAllOptions();

        for(MenuOptionItem menuOptionItem: menuOptions.getAllOptions()){
            System.out.println(menuOptionItem.getNumber() + menuOptionItem.getName());
        }
    }

    public void listAllBooks() {

        for(Book b: manager.getAllBooks()){
            System.out.println(b.getName());
        }

    }


    public void checkoutBook(String name) {
        if(manager.checkoutBook(name)){
            System.out.println("Thank you! Enjoy the book.");
        } else {
            System.out.println("That book is not available.");
        }
    }

    public void returnTheBook(String name) {
        if(manager.returnTheBook(name)){
            System.out.println("Thank you for returning the book.");
        } else {
            System.out.println("That is not a valid book to return.");
        }
    }

    public void checkoutBook() {
        System.out.println("Enter the name of the book to checkout: ");
        Scanner scannerCheckout = new Scanner(System.in);
        String bookToCheckout = scannerCheckout.nextLine();
        this.checkoutBook(bookToCheckout);
        this.listAvaiableBooks();
    }

    public void returnBook(){
        System.out.println("Enter the name of the book to return: ");
        Scanner scannerReturn = new Scanner(System.in);
        String bookToReturn = scannerReturn.nextLine();
        this.returnTheBook(bookToReturn);
    }


    public int optionMenu() {
        System.out.println("--------------------");
        System.out.println("Please, Select one option listed bellow: ");
        this.listAllInitialOptions();
        Scanner scanner = new Scanner(System.in);
        int option = Integer.parseInt(scanner.nextLine());
        return option;
    }

    public void printLinesInMenu(){
        System.out.println("--------------------");
    }

    public void buildMenu(){
        this.createABibliotecaManager();
        this.sayHello();
        this.printLinesInMenu();
        this.listAvaiableBooks();
        boolean exit = false;
        while(!exit){

            switch (this.optionMenu()){
                case 1: this.listAllBooks();
                    break;
                case 2: this.listAvaiableBooks();
                    break;
                case 3: this.checkoutBook();
                    break;
                case 4: this.returnBook();
                    break;
                case 5: this.listAvaiableMovies();
                    break;
                case 6:
                    exit = true;
                    break;
            }

        }
    }

}
